import sys
import os
import ctypes
import time
import platform

class xIPCLoader:
	dll_obj  = None
	xIPC_inited = False
	def __init__(self, dll_path):
		if (dll_path is None) or (not os.path.isfile(dll_path)):
			#print("parameter error")
			return
		self.dll_obj = ctypes.cdll.LoadLibrary(dll_path)
		return
	def get(self):
		return self.dll_obj
	def init_xIPC(self):
		if self.dll_obj is None:
			return False
		ret = self.dll_obj.xIPC_Init()
		if ret != 0:
			return False
		else:
			self.xIPC_inited = True
			return True
	def deinit_xIPC(self):
		if not self.xIPC_inited :
			return True
		if self.dll_obj is None:
			return True
		ret = self.dll_obj.xIPC_Deinit()
		if ret != 0:
			return False
		else:
			self.xIPC_inited = False
			return True

class Router:
	xIPC_obj  = None
	router_obj  = None
	def __init__(self, xIPC_obj):
		self.xIPC_obj = xIPC_obj
	def open(self, ip, port, thisSideName):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None:
			return False
		self.router_obj = (dll_obj.xIPC_Router_Get(ip.encode("UTF-8"),
		                                      port,
		                                      thisSideName.encode("UTF-8")))
		if self.router_obj == 0:
			return False
		else:
			return True
	def close(self):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None:
			return True
		eRet = dll_obj.xIPC_Router_Put(self.router_obj)
		if eRet == 0:
			return True
		else:
			return False

class Pipe:
	xIPC_obj  = None
	pipe_obj  = None
	def __init__(self, xIPC_obj):
		self.xIPC_obj = xIPC_obj
	def open(self, ip, port, thisSideName, listenRemote):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None:
			return False
		self.pipe_obj = (dll_obj.xIPC_Pipe_Get(ip.encode("UTF-8"),
		                                      port,
		                                      thisSideName.encode("UTF-8"),
		                                      listenRemote.encode("UTF-8")))
		if self.pipe_obj == 0:
			return False
		else:
			return True
	def close(self):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None:
			return True
		eRet = dll_obj.xIPC_Pipe_Put(self.pipe_obj)
		if eRet == 0:
			return True
		else:
			return False
	def sendstring(self, content, dest):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None or self.pipe_obj is None:
			return -1
		ret = dll_obj.xIPC_Pipe_Send(self.pipe_obj,
		                             dest.encode("UTF-8"),
		                             content.encode("UTF-8"),
		                             len(content))
		if ret <= 0:
			return 0
		else:
			return ret
	def readbytes(self, cnt):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None or self.pipe_obj is None:
			return None
		if cnt < 0:
			return None
		if cnt == 0:
			return b"";
		buf = ctypes.create_string_buffer(cnt)
		ret = dll_obj.xIPC_Pipe_Read(self.pipe_obj,
		                             buf,
		                             cnt)
		if ret <= 0:
			return None
		else:
			return buf.value #byte array
	def readstring(self, cnt):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None or self.pipe_obj is None:
			return None
		if cnt < 0:
			return None
		if cnt == 0:
			return "";
		buf = ctypes.create_string_buffer(cnt)
		ret = dll_obj.xIPC_Pipe_Read(self.pipe_obj,
		                             buf,
		                             cnt)
		if ret <= 0:
			return None
		else:
			return buf.value.decode("UTF-8")
	def waitbytes(self, cnt, timeout):
		dll_obj = self.xIPC_obj.get()
		if dll_obj is None or self.pipe_obj is None:
			return None
		if cnt < 0:
			return None
		if cnt == 0:
			return b"";
		buf = ctypes.create_string_buffer(cnt)
		ret = dll_obj.xIPC_Pipe_Wait(self.pipe_obj, buf, cnt, timeout)
		if ret <= 0:
			return None
		else:
			return buf.value #byte array